package cn.edu.suda.ada.parser;

import java.io.IOException;
import java.net.URISyntaxException;

import org.apache.http.client.ClientProtocolException;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import cn.edu.suda.ada.fetcher.PageFetcher;
import cn.edu.suda.ada.model.FetchedPage;
import cn.edu.suda.ada.model.Proxy;
import cn.edu.suda.ada.queue.StaticUrlQueue;
import cn.edu.suda.ada.queue.StaticVisitedUrlQueue;
import cn.edu.suda.ada.queue.UrlQueue;

public class GanJiUrlParser {
	/**
	 * 待爬url
	 */
	private UrlQueue urlQueue=new UrlQueue();
//	/**
//	 * 爬到的Url
//	 */
//	private UrlQueue urlParserQueue=new UrlQueue();
	private Elements links;
	private String baseUrl="";

	public GanJiUrlParser(){
		
	}
	
	public GanJiUrlParser(String city){
		this.baseUrl="http://"+city+".ganji.com";
	}
	
	public GanJiUrlParser(UrlQueue queue,String city){
		this.urlQueue.setUrlQueue(queue);
		this.baseUrl="http://"+city+".ganji.com";
	}
	
	
//	/**
//	 * 得到爬到的url队列
//	 * @method
//	 * @return UrlQueen
//	 * @throws IOException 
//	 * @throws ClientProtocolException 
//	 */
//	public void getUrlByParse() throws ClientProtocolException, IOException{
//		while(!this.urlQueue.isEmpty()){
//			String url=this.urlQueue.outElement();//从队列中拿出url
//			PageFetcher fetcher=new PageFetcher();
//			FetchedPage fetchedPage=null;
//			fetchedPage = fetcher.getHTMLbyProxy(url, pIp, pPort);
//			while(fetcher.isUsable(url)){
//				pIp=fetcher.getIP();
//				pPort=fetcher.getPort();
//				fetchedPage = fetcher.getHTMLbyProxy(url, pIp, pPort);
//				if("".equals(fetchedPage.getContent())||null==fetchedPage.getContent()||fetchedPage.getStatusCode()==403){
//					continue;
//				}
//				else {
//					break;
//				}
//			}
//			
//			this.parse(fetchedPage);//解析页面
//		}
//	}
	/**
	 * 从页面列表中获取房源URL
	 * @method
	 */
	public Object parse2(FetchedPage fetchedPage){
		Document doc = Jsoup.parse(fetchedPage.getContent());
		// 如果当前页面包含目标数据
		if(containsTargetData(fetchedPage.getUrl(), doc)){
			// 解析并获取目标数据
			// TODO	
			for(Element link:this.links){
				String urlString=link.attr("href").toString();
				if(!urlString.contains("http")){
					urlString=this.baseUrl+urlString;
				}
//				System.out.println("URL:"+urlString);
				StaticUrlQueue.addElement(urlString);
			}
		}
		
		// 将URL放入已爬取队列
//		StaticVisitedUrlQueue.addElement(fetchedPage.getUrl());
//		System.out.println();
		// 根据当前页面和URL获取下一步爬取的URLs
		// TODO
		
		return true;
	}
	
	/**
	 * 从页面列表中获取房源URL
	 * @method
	 */
	public UrlQueue parse(FetchedPage fetchedPage){
		Document doc = Jsoup.parse(fetchedPage.getContent());
		// 如果当前页面包含目标数据
		if(containsTargetData(fetchedPage.getUrl(), doc)){
			// 解析并获取目标数据
			// TODO	
			for(Element link:this.links){
				String urlString=link.attr("href").toString();
				if(!urlString.contains("http")){
					urlString=this.baseUrl+urlString;
				}
//				System.out.println("URL:"+urlString);
				urlQueue.addElement(urlString);
			}
		}
		
//		System.out.println("URLsParser:"+urlQueue.getUrlQueue());
		// 将URL放入已爬取队列
//		StaticVisitedUrlQueue.addElement(fetchedPage.getUrl());
//		System.out.println();
		// 根据当前页面和URL获取下一步爬取的URLs
		// TODO
		
		return urlQueue;
	}
	
	private boolean containsTargetData(String url, Document contentDoc){
		// 通过URL判断
		// TODO
		if(contentDoc.getElementsByAttributeValue("class", "list-info-title") != null){
			this.links=contentDoc.getElementsByAttributeValue("class", "list-info-title");
			return true;
		}
		
		return false;
	}
	
	public static void main(String[] args) throws ClientProtocolException, IOException{
		UrlQueue queue=new UrlQueue();
		queue.addElement("http://gz.ganji.com/xiaoqu/huashijiaoshixincun/chuzufang/f0");
//		GanJiUrlParser parser=new GanJiUrlParser(queue,"bj");
////		parser.getUrlByParse();
//		System.out.println(StaticUrlQueue.getUrlQueue());
		String url="http://gz.ganji.com/xiaoqu/huashijiaoshixincun/chuzufang/f0",host="gz.ganji.com";
		PageFetcher fetcher=new PageFetcher();
		FetchedPage page=fetcher.getHTMLbyProxy(url, Proxy.hostName[0], Proxy.port[0],host);
//		FetchedPage page=fetcher.getHTMLbyProxy(url, "117.136.234.242", 80, host);
		GanJiUrlParser parser=new GanJiUrlParser("gz");
		queue=parser.parse(page);
		System.out.println(queue.getUrlQueue());
	}
}
